<?php
require_once('../carbox.php');
if(!carbox_check_session( ) || $_SESSION['profile'] != 5)
	header('Location: http://'.$_SERVER['HTTP_HOST'].'/carbox/login.php');
	$query = cerberus_query(
		"SELECT usuario_id FROM Usuario WHERE login = :login",
		array(':login' => $_POST['login'])
	);
	$cerberus_user_id = (!$query["fail"] && !empty($query["result"]))? $query["result"][0]["usuario_id"] : NULL; 
	$query = carbox_query(
		"INSERT INTO User (login, password, profile_id, created_on) VALUES (:login, SHA2(SHA2(:password, 512), 512), :profile, NOW( ))",
		array(':login' => $_POST['login'], ':password' => $_POST['password'], ':profile' => $_POST['profile'])
	);
	$query = carbox_query(
		"SELECT id FROM User WHERE login = :login",
		array(':login' => $_POST['login'])
	);
	$query = carbox_query(
		"INSERT INTO CerberusUser (carbox_user_id, cerberus_user_id) VALUES (:cauid, :ceuid)",
		array(':cauid' => $query["result"][0]["id"], ':ceuid' => $cerberus_user_id)
	);
	if(!$query['fail'])
		echo json_encode(array('fail' => false, 'result' => 'Operação realizada com sucesso!'));
	else echo json_encode(array('fail' => true, 'result' => 'Falha na realização da operação!'));
?>
